var fs = require('fs');
var async = require('async');
var path = require('path');
var log = require('../model/log');

exports.upload = function(req, res, connection) {
	console.log("upload");
	var path = req.files.Filedata.path;
	var size = req.files.Filedata.size;
	var fileid = path.substring(path.length - 32, path.length);
	var temp = {
		path: path,
		name: req.files.Filedata.name,
		size: size,
		tempid: fileid
	};
	res.send(temp);
};

exports.show = function(req, res, connection) {

	console.log("show");
	var queryId = req.query.id;
	var getDir = "./files/" + queryId;
	console.log(getDir);
	var get = [];
	var count = 0;
	fs.exists(getDir, function(check) {
		if (check == true) {
			fs.readdir(getDir, function(err, files) {
				console.log(files);
				async.whilst(function() {
					return count < files.length;
				}, function(cb) {
					var data = fs.readFileSync(getDir + '/' + files[count]);
					get[count] = {
						"name": files[count],
						"size": parseFloat(data.length / 1024)
					};
					console.log(get[count]);
					count++;
					cb();
				}, function(err) {
					if (err != undefined)
						console.log(err);
				});
				res.send(get);
			});
		} else {
			res.send("cannot get the file");
		}
	});
};
//下载文件
exports.download = function(req, res) {
	// console.log(req.query.employeeId);
	// console.log(req.query.fileName);
	var getId = req.query.employeeId;
	var getName = req.query.fileName;
	var pathname = "./files/" + getId + "/" + getName;

	var types = {
		"css": "text/css",
		"gif": "image/gif",
		"html": "text/html",
		"ico": "image/x-icon",
		"jpeg": "image/jpeg",
		"jpg": "image/jpeg",
		"js": "text/javascript",
		"json": "application/json",
		"pdf": "application/pdf",
		"png": "image/png",
		"svg": "image/svg+xml",
		"swf": "application/x-shockwave-flash",
		"tiff": "image/tiff",
		"txt": "text/plain",
		"wav": "audio/x-wav",
		"wma": "audio/x-ms-wma",
		"wmv": "video/x-ms-wmv",
		"xml": "text/xml",
		"zip": "application/zip"
	};
	var fileText = getName.substring(getName.lastIndexOf("."), getName.length);
	var fileAfterName = fileText.toLowerCase(); //获取到文件后缀名
	path.exists(pathname, function(exists) {
		if (!exists) {
			res.writeHead(404, {
				'Content-Type': 'text/plain'
			});
			response.write("This request URL " + pathname + " was not found on this server.");
			response.end();
		} else {
			fs.readFile(pathname, "binary", function(err, file) {
				if (err) {
					res.writeHead(500, {
						'Content-Type': 'text/plain'
					});
					res.end(err);
				} else {
					res.header('Content-Disposition', 'attachment;filename=' + encodeURIComponent(getName));
					res.writeHead(200, {
						'Content-Type': types.fileAfterName
					});
					res.write(file, "binary");
					res.end();
				}
			});
		}
	});
	console.log("download");
};

//删除文件
exports.destroy = function(req, res, connection) {

	console.log("destroy");
	var getId = req.query.employeeId;
	var getName = req.query.fileName;
	var getDir = './files/' + getId + '/' + getName;
	var content = {
		fileName: getName,
		employeeId: getId
	};
	var content = JSON.stringify(content);
	fs.unlink(getDir, function() {
		var logData = {
			itemId: getId,
			updatePersonId: req.user.employeeId,
			content: content,
			type: '2',
			operation: '删除'
		};
		log.addLog(connection, logData, function(log) {
			console.log('log', log);
			if (log != null && log != undefined)
				console.log('add log success');
		});
		console.log("delete file success.");
		res.send("delete file success.");

	});
};